package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.util.DateUtils;
import com.laoxuai.util.StringUtils;
import com.music.dao.UserDao;
import com.music.pojo.User;
import com.music.service.UserService;
import com.music.util.PasswordEncoder;
import com.music.util.PasswordUtil;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public int insert(User user) {
        try {
            user.setAuth(1);
            user.setLevel(1);
            user.setIsDeleted(1);
            String salt=PasswordUtil.getSalt();
            user.setSalt(salt);
            user.setPassword(PasswordUtil.encode("123456",salt));
            user.setCreateTime(new java.sql.Date(new Date().getTime()));
            return userDao.insert(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public int delete(Integer id) {
        try {
            return userDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(User user) {
        try {
            if(userDao.getOtherUserByName(user.getName(),user.getUserId())!=null){
                return -1;
            }
            return userDao.update(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updatePassword(User user) {
        try {
            user.setPassword(PasswordUtil.encode(user.getPassword(),user.getSalt()));
            return userDao.update(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User getByPrimaryKey(Integer id) {
        try {
            User user=userDao.getByPrimaryKey(id);
            user.setPassword(null);
            user.setSalt(null);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User getUserByName(String name) {
        try {
            return userDao.getUserByName(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User modifyLogin(String username, String password) {
        User user=getUserByName(username);
        if(user==null){
            return null;
        }
        if(user.getAuth()!=2||user.getIsDeleted()!=1){
            return null;
        }
        password=PasswordUtil.encode(password,user.getSalt());
        return user.getPassword().equals(password)?user:null;
    }

    @Override
    public List<User> listPage(User user, int page, int size) {
        try {
            return userDao.listPage(user, page, size);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<User> listPageInfo(User user, int page, int size) {
        try {
            return userDao.listPageInfo(user, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectByExample(User user) {
        Example example = createExmaple(user);
        try {
            return userDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }



    private Example createExmaple(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(user != null){
            if(!StringUtils.isEmpty(user.getUserId())){
                criteria.andEqualTo("userId", user.getUserId());
            }
            if(!StringUtils.isEmpty(user.getName())){
                criteria.andLike("name", "%"+user.getName()+"%");
            }
            if(!StringUtils.isEmpty(user.getPhone())){
                criteria.andEqualTo("phone", user.getPhone());
            }
            if(!StringUtils.isEmpty(user.getPassword())){
                criteria.andEqualTo("password", user.getPassword());
            }
            if(!StringUtils.isEmpty(user.getLevel())){
                criteria.andEqualTo("level", user.getLevel());
            }
            if(!StringUtils.isEmpty(user.getAvatarUrl())){
                criteria.andEqualTo("avatarUrl", user.getAvatarUrl());
            }
            if(!StringUtils.isEmpty(user.getCreateTime())){
                criteria.andEqualTo("createTime", user.getCreateTime());
            }
            if(!StringUtils.isEmpty(user.getUpdateTime())){
                criteria.andEqualTo("updateTime", user.getUpdateTime());
            }
            if(!StringUtils.isEmpty(user.getIsDeleted())){
                criteria.andEqualTo("isDeleted", user.getIsDeleted());
            }
        }
        return example;
    }

    /**
     * User 构建查询对象
     * @param user
     * @return
     */
    public Example createExample(User user){
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if(user != null){
            if(!StringUtils.isEmpty(user.getUserId())){
                criteria.andEqualTo("userId", user.getUserId());
            }
            if(!StringUtils.isEmpty(user.getName())){
                criteria.andLike("name", "%"+user.getName()+"%");
            }
            if(!StringUtils.isEmpty(user.getPhone())){
                criteria.andEqualTo("phone", user.getPhone());
            }
            if(!StringUtils.isEmpty(user.getPassword())){
                criteria.andEqualTo("password", user.getPassword());
            }
            if(!StringUtils.isEmpty(user.getLevel())){
                criteria.andEqualTo("level", user.getLevel());
            }
            if(!StringUtils.isEmpty(user.getAvatarUrl())){
                criteria.andEqualTo("avatarUrl", user.getAvatarUrl());
            }
            if(!StringUtils.isEmpty(user.getCreateTime())){
                criteria.andEqualTo("createTime", user.getCreateTime());
            }
            if(!StringUtils.isEmpty(user.getUpdateTime())){
                criteria.andEqualTo("updateTime", user.getUpdateTime());
            }
            if(!StringUtils.isEmpty(user.getIsDeleted())){
                criteria.andEqualTo("isDeleted", user.getIsDeleted());
            }
        }
        return example;
    }
}
